''' A library that provides a python interface to the grooveshark API '''
''' All references to grooveshark API can be found at www.gsuser.com'''

__author__ = 'yusuf.mahdi@gmail.com'
_version__ = '1.0-devel'

import urllib
import urllib2
import simplejson as json


class gsSong():
    '''A class representing the song structure user by grooveshark API.'''
    def __init__(self,title,description,link):
        self.title = title
        self.description = description
        self.link = link
    def printSong(self):
        print self.title
        print self.description
        print self.link
        
class gsUser():
    '''A class representing the user structure by the grooveshark API.'''
    def __init__(self,username,profileURL,userID,forumURL):
        self.username = username
        self.profileURL = profileURL
        self.userID = userID
        self.forumURL = forumURL
    def printUser(self):
        print self.username 
        print self.userID
        print self.profileURL 
        print self.forumURL
    
        
''' This method returns the last song listened to by user. '''

def getLastSong(user):
    base_url = 'http://gsuser.com/lastSong/'
    user_url = base_url + '%s/%d/' % (user,1)
    raw = urllib.urlopen(user_url)
    json_object = json.load(raw)
   
    
   #filtering and making it look good. 
  
    song = json_object[0]
    title = song["title"]
    description = song["description"]
    link = song["link"]    
    gsong = gsSong(title,description,link)
    
    return gsong.printSong()
    
''' This method returns link to grooveshark profile of user. '''
def getProfile(user):
    base_url = 'http://gsuser.com/profile/'
    user_url = base_url +  '%s/' % (user)
    raw = urllib.urlopen(user_url)
    profile = raw.read()
    raw.close()
    print profile

''' This method returns link to grooveshark forum profile of user. '''
def getForumProfile(user):
    base_url = 'http://gsuser.com/forumProfile/'
    user_url = base_url + '%s/' %(user)
    raw = urllib.urlopen(user_url)
    forum_profile = raw.read()
    raw.close()
    print forum_profile

''' This method returns link to grooveshark profile picture of user. '''
def getPic(user):
    base_url = 'http://gsuser.com/pic/'
    user_url = base_url + '%s/%s/' %(user,'m')
    print user_url

def getUser(user):
    base_url = 'http://gsuser.com/user/'
    user_url = base_url + '%s/' %(user)
    raw = urllib.urlopen(user_url)
    json_object = json.load(raw)
    user = json_object
    username = user["Username"]
    profile_url = user["ProfileURL"]
    user_id = user['UserID']
    forum_url = user ['ForumProfileURL']
    gsuser = gsUser(username,profile_url, user_id, forum_url)
    return gsuser.printUser()
def getSharky():
    return "sharky was successfully installed. I never liked jaws"
        
        

